from re import I
from pymongo import MongoClient
import json
import os
mongourl = "mongodb://yuqing:Bigdata#2021@175.27.160.162:27017"
client = MongoClient(mongourl)
db = client['Task']
collection_task = db['task']
weixin_info_list = []
weibo_info_list = []

def get_weixin_info():
    resultlist = collection_task.find({'status':'wait','source':'微信'})
    if resultlist:
    # print(resultlist)
        for result in resultlist:
            weixin_info_list.append(result['canshu'])
        return weixin_info_list
def get_weibo_info():
    resultlist = collection_task.find({'status':'wait','source':'微博'})
    if resultlist:
        for result in resultlist:
            weibo_info_list.append(result['canshu'])
        return weibo_info_list    
  
def runwxspider(weixin_info_list):    
    try:
        if weixin_info_list:
            for info in weixin_info_list:
                condition = {'canshu':info}
                task_info = collection_task.find_one(condition)
                id = task_info['_id'] 
                # print('参数',info)
                info_json = json.loads(info)
                # print(cookie,collection_name,keyword,id)
                os.system('nohup python -u weixinspider.py -i {} > /home/ubuntu/spiderslogs/{}.logs 2>&1 &'.format(id,id))
                # os.system('nohup python3 weixinspider.py -c {} -k {} -n {} -i {}'.format(cookie,keyword,collection_name,id))
                # result = collection_task.update_one(condition,{'$set':{'status':'run'}})
                # print(result.matched_count,result.modified_count)
    except:
        print('没有查询到需要运行的微信任务')

def runweibospider(weibo_info_list):
    try:
        if weibo_info_list:
            for info in weibo_info_list:
                condition = {'canshu':info}
                task_info = collection_task.find_one(condition)
                id = task_info['_id']
                os.system('nohup python -u weibo_kwd_api.py -i {} > /home/ubuntu/spiderslogs/{}.logs 2>&1 &'.format(id,id))
    except:
        print('没有查询到需要运行的微博任务')

if __name__ == '__main__':
    get_weixin_info()
    get_weibo_info()
    runwxspider(weixin_info_list)
    runweibospider(weibo_info_list)
        
    

